package com.songcan.oauth.service.impl;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songcan.common.exception.ObjectNotException;
import com.songcan.model.oauth.SysRole;
import com.songcan.model.oauth.SysRoleMenu;
import com.songcan.oauth.mapper.SysRoleMapper;
import com.songcan.oauth.service.SysRoleMenuService;
import com.songcan.oauth.service.SysRoleService;
import com.songcan.oauth.vo.Jurisdiction;
import com.songcan.oauth.vo.RoleVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 系统角色表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-04-29
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private SysRoleMenuService sysRoleMenuService;


    @Autowired
    public SysRoleServiceImpl(SysRoleMenuService sysRoleMenuService){
        this.sysRoleMenuService = sysRoleMenuService;
    }

    @Override
    public RoleVO selectByDetail(Long id) {
        SysRole sysRole = getById(id);
        if(Objects.isNull(sysRole)){
            throw new ObjectNotException("角色不存在");
        }

        List<Jurisdiction> list = sysRoleMenuService.selectByRoleMenu(id);
        return  new RoleVO(sysRole,list);
    }

    @Override
    public void addRole(String name, String roleCode, String roleDesc, String... menuIds) {
        Long id = IdWorker.getId();
        SysRole sysRole = new SysRole(id,name,roleCode,roleDesc,new Date(),null,0);
        save(sysRole);
        List<SysRoleMenu> ids =  Arrays.stream(menuIds).map(p->new SysRoleMenu(id,Long.valueOf(p))).collect(Collectors.toList());
        sysRoleMenuService.saveBatch(ids);
    }

    @Override
    public void updateRole(Long roleId, String name, String roleCode, String roleDesc, String... menuIds) {
        SysRole sysRole = getById(roleId);
        if(Objects.isNull(sysRole)){
            throw new ObjectNotException("角色不存在");
        }
        sysRole.setRoleName(name);
        sysRole.setRoleCode(roleCode);
        sysRole.setRoleDesc(roleDesc);
        update(sysRole,Wrappers.lambdaUpdate(new SysRole()).eq(SysRole::getRoleId,roleId));
        sysRoleMenuService.remove(Wrappers.lambdaQuery(new SysRoleMenu()).eq(SysRoleMenu::getRoleId,roleId));
        List<SysRoleMenu> ids =  Arrays.stream(menuIds).map(p->new SysRoleMenu(sysRole.getRoleId(),Long.valueOf(p))).collect(Collectors.toList());
        sysRoleMenuService.saveBatch(ids);
    }

    @Override
    public IPage<SysRole> selectByRolePage(Page page, String name) {
        return baseMapper.selectByRolePage(page,name);
    }
}
